<!DOCTYPE html>
<html>
<!--
Copyright 2009 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<!--
  Author: arv@google.com (Erik Arvidsson)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>
  Closure Unit Tests - goog.gears.MultipartFormData
</title>
<script src="../base.js"></script>
<script>

goog.require('goog.gears.MultipartFormData');
goog.require('goog.testing.MockControl');
goog.require('goog.testing.jsunit');

</script>
</head>
<body>
<script>

var mockControl = new goog.testing.MockControl;
var propertyReplacer = new goog.testing.PropertyReplacer;
var mockFactory;
var mockBlobBuilder = {
  sb: [],
  append: function(s) {
    this.sb.push(s);
  },
  getAsBlob: function() {
    return this.sb.join('');
  },
  reset: function() {
    this.sb.length = 0;
  }
};
var RANDOM = '123abc'

function setUp() {
  mockFactory = {
    create: function(id) {
      assertEquals('beta.blobbuilder', id);
      return mockBlobBuilder;
    }
  };
  propertyReplacer.set(goog.string, 'getRandomString',
                       mockControl.createFunctionMock());
  propertyReplacer.set(goog.gears, 'getFactory', function() {
    return mockFactory;
  });

  goog.string.getRandomString().$returns(RANDOM);

  mockControl.$replayAll();
};

function tearDown() {
  mockControl.$verifyAll();

  mockControl.$resetAll();
  mockBlobBuilder.reset();
  propertyReplacer.reset();
}

function testConstruct() {
  assertEquals('object', typeof new goog.gears.MultipartFormData());
}

function testNoAddsShouldResultInEmptyBlob() {
  var data = new goog.gears.MultipartFormData();
  assertEquals('', data.getAsBlob());
}

function testAddText() {
  var data = new goog.gears.MultipartFormData();
  data.addText('name1', 'value1');
  var expected = '------123abc\r\n' +
      'Content-Disposition: form-data; name="name1"\r\n' +
      'Content-Type: text/plain; charset=UTF-8\r\n' +
      '\r\n' +
      'value1\r\n' +
      '------123abc--\r\n';

  assertEquals(expected, data.getAsBlob());
}

function testAddTextWithNumber() {
  var data = new goog.gears.MultipartFormData();
  data.addText('name1', 123);
  var expected = '------123abc\r\n' +
      'Content-Disposition: form-data; name="name1"\r\n' +
      'Content-Type: text/plain; charset=UTF-8\r\n' +
      '\r\n' +
      '123\r\n' +
      '------123abc--\r\n';

  assertEquals(expected, data.getAsBlob());
}

function testAddTextMultiple() {
  var data = new goog.gears.MultipartFormData();
  data.addText('name1', 'value1');
  data.addText('name2', 'value2');
  var expected = '------123abc\r\n' +
      'Content-Disposition: form-data; name="name1"\r\n' +
      'Content-Type: text/plain; charset=UTF-8\r\n' +
      '\r\n' +
      'value1\r\n' +
      '------123abc\r\n' +
      'Content-Disposition: form-data; name="name2"\r\n' +
      'Content-Type: text/plain; charset=UTF-8\r\n' +
      '\r\n' +
      'value2\r\n' +
      '------123abc--\r\n';

  assertEquals(expected, data.getAsBlob());
}

function testAddFile() {
  var gearsFile = {
    name: 'FILENAME',
    blob: 'BLOB'
  };

  var data = new goog.gears.MultipartFormData();
  data.addFile('name1', gearsFile);
  var expected = '------123abc\r\n' +
      'Content-Disposition: form-data; name="name1"; filename="FILENAME"\r\n' +
      'Content-Type: application/octet-stream\r\n' +
      '\r\n' +
      'BLOB\r\n' +
      '------123abc--\r\n';

  assertEquals(expected, data.getAsBlob());
}

function testAddBlob() {
  var data = new goog.gears.MultipartFormData();
  data.addBlob('name1', 'FILENAME', 'BLOB');
  var expected = '------123abc\r\n' +
      'Content-Disposition: form-data; name="name1"; filename="FILENAME"\r\n' +
      'Content-Type: application/octet-stream\r\n' +
      '\r\n' +
      'BLOB\r\n' +
      '------123abc--\r\n';

  assertEquals(expected, data.getAsBlob());
}

function testAddMultiple() {
  var gearsFile = {
    name: 'FILENAME2',
    blob: 'BLOB2'
  }
  var data = new goog.gears.MultipartFormData();
  data.addText('name1', 'value1');
  data.addFile('name2', gearsFile);
  data.addBlob('name3', 'FILENAME3', 'BLOB3');

  var expected = '------123abc\r\n' +
      'Content-Disposition: form-data; name="name1"\r\n' +
      'Content-Type: text/plain; charset=UTF-8\r\n' +
      '\r\n' +
      'value1\r\n' +
      '------123abc\r\n' +
      'Content-Disposition: form-data; name="name2"; filename="FILENAME2"\r\n' +
      'Content-Type: application/octet-stream\r\n' +
      '\r\n' +
      'BLOB2\r\n' +
      '------123abc\r\n' +
      'Content-Disposition: form-data; name="name3"; filename="FILENAME3"\r\n' +
      'Content-Type: application/octet-stream\r\n' +
      '\r\n' +
      'BLOB3\r\n' +
      '------123abc--\r\n';

  assertEquals(expected, data.getAsBlob());
}

function testGetContentType() {
  var data = new goog.gears.MultipartFormData();
  assertEquals('multipart/form-data; boundary=' +
               '----' + RANDOM,
               data.getContentType());
}

function testAddAfterClose() {
  var data = new goog.gears.MultipartFormData();
  data.addText('name1', 'value1');
  var expected = '------123abc\r\n' +
      'Content-Disposition: form-data; name="name1"\r\n' +
      'Content-Type: text/plain; charset=UTF-8\r\n' +
      '\r\n' +
      'value1\r\n' +
      '------123abc--\r\n';

  assertEquals(expected, data.getAsBlob());

  assertThrows(function() {
    data.addText('name2', 'value2');
  });
}

function testInvalidValue() {
  var data = new goog.gears.MultipartFormData();
  assertThrows(function() {
    data.addText('name1', data.boundary_);
  });
}


</script>
</body>
</html>
